package com.swiftops.basic.sys.controller;

import com.swiftops.basic.sys.service.SysRoleService;
import com.swiftops.basic.sys.vo.entity.SysRoleEntityVO;
import com.swiftops.basic.sys.vo.request.SysRoleRequestVO;
import com.swiftops.basic.sys.vo.response.SysRoleResponseVO;
import com.swiftops.common.core.ApiResult;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.BindingResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 角色
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1/sys")
@Api(description = "角色")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("获取角色分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysRole", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<PageResult<SysRoleResponseVO>>> getSysRolePageList(SysRoleRequestVO requestVO) {
        try {
            PageResult<SysRoleResponseVO> pageResult = this.sysRoleService.querySysRolePageList(requestVO);
            return ApiResult.ok(pageResult);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("获取角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysRole/{id}", method = RequestMethod.GET)
    public ResponseEntity<ApiResult<SysRoleEntityVO>> getSysRole(@PathVariable("id") String id) {
        try {
            SysRoleEntityVO sysRoleEntityVO = this.sysRoleService.getSysRoleEntityVO(id);
            return ApiResult.ok(sysRoleEntityVO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysRole", method = RequestMethod.POST)
    public ResponseEntity<ApiResult<Object>> addSysRole(@RequestBody @Validated SysRoleEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.sysRoleService.addSysRole(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }

    @ApiOperation("更新角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysRole", method = RequestMethod.PUT)
    public ResponseEntity<ApiResult<Object>> updateSysRole(@RequestBody @Validated SysRoleEntityVO entityVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.getErrorMessage(bindingResult);
        }
        try {
            this.sysRoleService.updateSysRole(entityVO);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }


    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Authorization", required = false, dataType = "string", paramType = "header"),
    })
    @RequestMapping(value = "/sysRole/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ApiResult<Object>> deleteSysRole(@PathVariable("id") String id) {
        try {
            this.sysRoleService.deleteSysRole(id);
            return ApiResult.ok();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ApiResult.error(e);
        }
    }
}
